package com.future.mayi.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.model.CouponsModel;
import com.future.mayi.utils.PriceUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class CouponsAdapter extends BaseRecyclerViewAdapter<CouponsModel> {


    /**
     * type
     * 0-未使用 1-已使用 10-已过期
     * 4领取
     * 2选择优惠券
     */
    private int type;

    private int itemH;
    private int textW;

    public CouponsAdapter(Context context, XRecyclerView xRecyclerView, int type) {
        super(context, null, xRecyclerView);
        this.type = type;
        itemH = (int) ((CommonUtils.getScreenSize(context)[0] - (type == 2 ? CommonUtils.dip2px(context, 30) : 0)) * 0.2689f) + CommonUtils.dip2px(context, 15);
        textW = (CommonUtils.getScreenSize(context)[0] - CommonUtils.dip2px(context, 30)) / CommonUtils.px2dip(mContext, 344) * CommonUtils.px2dip(mContext, type == 4 ? 70 : 40);
    }

    @Override
    public int getItemViewType(int position) {
        if (getItem(position) == null && position == 0)
            return 11;
        else return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (type == 2)
            return new ItemViewHolder(getView(parent, R.layout.layout_coupons_select_item));
        else if (type == 1)
            return new ItemViewHolder(getView(parent, R.layout.layout_coupons_use_item));
        else if (type == 10)
            return new ItemViewHolder(getView(parent, R.layout.layout_coupons_old_item));
        else if (type == 4)
            return new ItemViewHolder(getView(parent, R.layout.layout_coupons_get_item));
        else
            return new ItemViewHolder(getView(parent, R.layout.layout_coupons_item));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        ItemViewHolder holder = (ItemViewHolder) viewHolder;
        ViewGroup.LayoutParams params = holder.rlView.getLayoutParams();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = itemH;

        CouponsModel model = getItem(position);
        if (type == 4) {
            if (mOnItemClickListener != null)
                holder.tvGet.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.OnItem(v, position);
                    }
                });

            ViewGroup.LayoutParams tvLp = holder.tvGet.getLayoutParams();
            tvLp.width = textW;
            tvLp.height = ViewGroup.LayoutParams.MATCH_PARENT;
            if (model.isSelect()) {
                setText(holder.tvGet, R.string.get_coupons_did);
            } else {
                setText(holder.tvGet, R.string.get_coupons);
            }

            setText(holder.tvTime, "有效期：领取后" + model.getExpiry() + "天内");
        } else {
            if (mOnItemClickListener != null)
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.OnItem(v, position);
                    }
                });
        }


        if (type == 4||type == 0) {
            if (mOnItemClickListener != null)
                holder.tvGet.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.OnItem(v, position);
                    }
                });
        }
        if (type == 2) {
            holder.ivSelect.setImageResource(model.isSelect() ? R.drawable.tab_selected : R.drawable.tab_normal);
        }
        setText(holder.tvNum, PriceUtils.getPriceText(model.getPar_value()));
        setText(holder.tvConditions, "满" + PriceUtils.getPriceText(model.getUse_condition()) + "可使用");
        setText(holder.tvTitle, model.getCoupon_name());
        if (type != 4) setText(holder.tvTime, model.getCreatetime() + "-" + model.getExpirytime());

    }


    class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.rl_view)
        View rlView;
        @BindView(R.id.tv_num)
        TextView tvNum;
        @BindView(R.id.tv_conditions)
        TextView tvConditions;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_get)
        TextView tvGet;
        ImageView ivSelect;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            if (type == 2)
                ivSelect = (ImageView) itemView.findViewById(R.id.iv_select);
        }
    }


    @Override
    public void setList(List<CouponsModel> items) {
        super.setList(items);
    }

    public void setType(int type) {
        this.type = type;
    }
}
